package freespiritsocial.io.user.ui.helper

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.animation.doOnEnd
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import freespiritsocial.io.user.R
import freespiritsocial.io.user.data.model.bean.PostLocationPojo
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.ext.util.loge

object MarkerHelper {
    private const val MARK_COUNTRY = 1
    private const val MARK_PROVINCE = 2
    private const val MARK_CITY = 3
    private const val MARK_AREA = 4
    private const val MARK_OTHER = 5

    private val markerListMap = hashMapOf<Int,ArrayList<Marker>>(
        MARK_CITY to arrayListOf(),
        MARK_PROVINCE to arrayListOf(),
        MARK_COUNTRY to arrayListOf(),
        MARK_AREA to arrayListOf(),
        MARK_OTHER to arrayListOf(),
    )

    private val postLocationList = arrayListOf<PostLocationPojo>()

    fun parseData(data: List<PostLocationPojo>) {
        postLocationList.clear()
        postLocationList.addAll(data)
    }

    fun parseMarker(map: GoogleMap,data: List<PostLocationPojo> = postLocationList) {
        map.clear()
        for (mutableEntry in markerListMap) {
            mutableEntry.component2().clear()
        }
        data.forEach {
            addMarker(map,it)
        }
    }

    fun changeMarkerIcon(isSelect: Boolean,masker: Marker?) {
        masker?.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(masker.tag as? String ?: "",isSelect)))
    }

    private fun addMarker(map: GoogleMap,bean: PostLocationPojo) {
        map.addMarker(
            MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(bean.name))).position(LatLng(bean.lat,bean.lng))
        )?.apply {
            this.tag = bean.name
            markerListMap[bean.type]?.add(this)
            alpha = if (canShowMarkerType(map.cameraPosition.zoom) == bean.type) 1f else 0f
        }
    }

    private fun canShowMarkerType(zoom: Float): Int {
        return when (zoom) {
            in 0f..7f -> MARK_COUNTRY
            in 7f..10f -> MARK_CITY
            in 10f..14f -> MARK_AREA
            else -> MARK_OTHER
        }
    }

    private fun createDrawableFromView(name: String,isSelect: Boolean = false): Bitmap {
        val view = (appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(
            if (isSelect) R.layout.layout_marker_select else R.layout.layout_marker,
            null
        )
        view?.findViewById<TextView>(R.id.tv_name)?.text = name
        view.measure(
            View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED)
        )
        view.layout(0,0,view.measuredWidth,view.measuredHeight)
        val bp = Bitmap.createBitmap(view.measuredWidth,view.measuredHeight,Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bp)
        view.draw(canvas)
        canvas.save()
        return bp
    }

    /**
     * 当前地图缩放
     */
    var curZoom = 9.3f
    fun changeZoom(zoom: Float) {
        val curType = canShowMarkerType(curZoom)
        val newType = canShowMarkerType(zoom)
        showMarkers(newType,curType)
        curZoom = zoom
        curZoom.toString().loge("curZoom")
    }

    private fun showMarkers(newType: Int,curType: Int) {
        if (newType == curType) return
        markerListMap[newType]?.forEach {
            val ofFloat = ValueAnimator.ofFloat(0f,1f)
            ofFloat.duration = 300
            ofFloat.addUpdateListener { value ->
                val alpha = value.animatedValue as Float
                it.alpha = alpha
            }
            ofFloat.doOnEnd {
                it.removeAllListeners()
            }
            ofFloat.start()
        }
        markerListMap[curType]?.forEach {
            val ofFloat = ValueAnimator.ofFloat(1f,0f)
            ofFloat.duration = 300
            ofFloat.addUpdateListener { value ->
                val alpha = value.animatedValue as Float
                it.alpha = alpha
            }
            ofFloat.doOnEnd {
                it.removeAllListeners()
            }
            ofFloat.start()
        }
    }
}